package cpi.controller;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import cpi.entity.Cpi;
import cpi.service.CpiService;
import cpi.service.PredictService;
import cpi.tools.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * (Cpi)表控制层
 *
 * @author makejava
 * @since 2022-07-07 11:25:07
 */
@CrossOrigin
@RestController
@RequestMapping("cpi")
public class CpiController extends ApiController {

    @Resource
    private CpiService cpiService;
    @Resource
    private PredictService predictService;


    @ApiOperation("获取所有cpi")
    @GetMapping("/selectAll")
    public CommonResult selectAll() {
        return CommonResult.success(cpiService.list());
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody Cpi cpi){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        String strDate = sdf.format(ts);
        cpi.setUpdateTime(Timestamp.valueOf(strDate)+"");
        return CommonResult.success(cpiService.add(cpi));
    }
    @ApiOperation("按省获取cpi")
    @GetMapping("/byprovince")
    public CommonResult byprovince(int page,int size,String type,int time) {
        return CommonResult.success(cpiService.byprovince(page,size,type,time));
    }

    @ApiOperation("按分类获取cpi")
    @GetMapping("/bytype")
    public CommonResult bytype(String province,int time) {
        return CommonResult.success(cpiService.bytype(province,time));
    }

    @ApiOperation("获取所有类别")
    @GetMapping("/alltype")
    public CommonResult alltype(){
        return CommonResult.success(cpiService.alltype());
    }

    @ApiOperation("获取所有省份")
    @GetMapping("/allprovince")
    public CommonResult allprovince(){
        return CommonResult.success(cpiService.allprovince());
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody Cpi cpi) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",cpi.getId());
        updateWrapper.set("cpi",cpi.getCpi());
        updateWrapper.set("type",cpi.getType());
        updateWrapper.set("province",cpi.getProvince());
        updateWrapper.set("represent_time",cpi.getRepresentTime());
        updateWrapper.set("update_time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()));
        updateWrapper.set("update_id",cpi.getUpdateId());
        return CommonResult.success(cpiService.update(updateWrapper));
    }

    @PostMapping("/remove")
    public CommonResult delete(int id) {
        return CommonResult.success(cpiService.removeById(id));
    }

    @GetMapping("/listPage")
    public  CommonResult listPage(int page,int size,String type,String province){
        Map<String,Object> map=new HashMap();
        map.put("data",cpiService.listPage(page,size,type,province));
        map.put("count",cpiService.counts(type,province));
        map.put("page",page);
        return CommonResult.success(map);
    }

    @GetMapping("/select")
    public CommonResult select(String province, String date, String type, Integer page, Integer size) {
        return cpiService.findCpiData(province, date, type, page, size);
    }

    @GetMapping("/predict")
    public CommonResult predict(String type,String province,Integer time,String plan){
        Map<String,Object> map=new LinkedHashMap<>();
        if(plan.indexOf("a")>=0){
            map.put("a",plan1(cpiService.predict(type,province,time),type,province));
        }
        if(plan.indexOf("b")>=0){
            map.put("b",plan2(cpiService.predict(type,province,time),type,province));
        }
        if(plan.indexOf("c")>=0){
            map.put("c",plan3(cpiService.predict(type,province,time),type,province));
        }
        return CommonResult.success(map);
    }

    public Map<String,Object> plan1(Map<String,Object> test,String type, String province){
        test.forEach((key, value) -> {
            try {
                String d = predictService.predictOneCpi("xgboost",type,province,formate(key));
                test.put(key,Double.parseDouble(d));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        });
        test.put("plan","XGBoost算法");
        return test;
    }

    public Map<String,Object> plan2(Map<String,Object> test,String type, String province){
        test.forEach((key, value) -> {
            try {
                String d = predictService.predictOneCpi("poly_svm",type,province,formate(key));
                test.put(key,Double.parseDouble(d));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        });
        test.put("plan","非线性SVM算法");
        return test;
    }
    public Map<String,Object> plan3(Map<String,Object> test,String type, String province){
        test.forEach((key, value) -> {
            try {
                String d = predictService.predictOneCpi("softmax_reg",type,province,formate(key));
                test.put(key,Double.parseDouble(d));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        });
        test.put("plan","Softmax回归分类预测算法");
        return test;
    }

    public static String formate(String date) {
        String[] splits = date.split("-");

        StringBuilder builder = new StringBuilder();

        int year = Integer.parseInt(splits[0]);

        builder.append(year);

        String month = splits[1];

        if (month != null && month.charAt(0) == '0') {
            month = String.valueOf(month.charAt(1));
        }

        builder.append("/" + month);

        builder.append("/1");

        return builder.toString();
    }
}

